package com.bocai.eelly.utils;


import android.content.Context;
import android.text.TextUtils;
import android.util.Base64;

import com.bocai.eelly.net.Url;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by kaizen on 16/4/28.
 */
public class Utils {

    public static final String EVENTMSG="photos";
    public static final int REQUEST_CODE=1000;
    public static final int PAGESIZE=10;

    public static boolean checkResultCode(int code) {
        if (0==code) {
            return true;
        } else {
            return false;
        }
    }
    public static String getUserId(Context context,String key){
        return SP.get(context,key,"0").toString();
    }
    public static String checkNull(String s) {
        return s == null ? "" : s;
    }
    public static String getTimeline(){
        SimpleDateFormat formatter    =   new    SimpleDateFormat    ("yyyyMMddHHmm");
        Date curDate    =   new    Date(System.currentTimeMillis());//获取当前时间
        String    str    =    formatter.format(curDate);
        return str;
    }

    public static String base64Encode(String str){
        return Base64.encodeToString(str.getBytes(), android.util.Base64.DEFAULT);
    }

    public static String md5(String string) {
        string=string+ Url.SIGN;
        byte[] hash;
        try {
            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Huh, MD5 should be supported?", e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Huh, UTF-8 should be supported?", e);
        }

        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10) hex.append("0");
            hex.append(Integer.toHexString(b & 0xFF));
        }
        return hex.toString();
    }

    public static boolean isEmpty(String str){
        if(TextUtils.isEmpty(str)){
            return true;
        }
        return false;
    }
}
